package com.tracking.application.activities;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.maps.model.Marker;
import com.tracking.application.DataBase.DbAdapter;
import com.tracking.application.R;
import com.tracking.application.fragments.GraphFragment;
import com.tracking.application.fragments.MapFragment;
import com.tracking.application.fragments.StatisticFragment;
import com.tracking.application.listeners.ActionBarTabListener;
import com.tracking.application.util.CommonModuleApp;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Kamil.O
 * Description: Activity with tabs map, statistics
 * Date: 18.11.13
 * Time: 23:07
 */
public class TabsActivity extends Activity{
    private static final String KEY_ID_ROUTE = "KEY_ID_ROUTE";

    private long idRoad = -1;
    private double firstLatitude;
    private double firstLongitude;
    private double firstAltitude;
    private boolean isFromHistory = false;
    private Marker lastMarker;
    private Marker firstMarker;
    private int mapMode = 1;
    private String redirectType;

    // Commons graph/stat
    private DbAdapter db;
    protected Map<String, List<Double>> dataList;
    protected List<Double> speedListKm = new ArrayList<Double>();
    protected List<Double> distanceListSumForGraphKm = new ArrayList<Double>();
    protected List<Double> altitudeListPrecision = new ArrayList<Double>();
    protected double maxSpeed = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(CommonModuleApp.TAG_LOG_CAT, "TabsActivity onCreate");

        isFromHistory = false;

        Bundle extras = getIntent().getExtras();
        this.idRoad = extras.getLong(KEY_ID_ROUTE);
        this.firstLatitude = extras.getDouble(CommonModuleApp.KEY_GPS_LAT);
        this.firstLongitude = extras.getDouble(CommonModuleApp.KEY_GPS_LONG);
        this.firstAltitude = extras.getDouble(CommonModuleApp.KEY_GPS_ALTITUDE);

        this.mapMode = extras.getInt(CommonModuleApp.KEY_MAP_MODE);
        this.redirectType = extras.getString(CommonModuleApp.KEY_TYPE_REDIRECT);

        if (extras.containsKey(CommonModuleApp.KEY_HISTORY_TO_MAP)){
            isFromHistory = true;
        }

        initializeActionBarTabs();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(CommonModuleApp.TAG_LOG_CAT, "TabsActivity onDestroy");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(CommonModuleApp.TAG_LOG_CAT, "TabsActivity onResume");
        db = new DbAdapter(this);

        if (isFromHistory()){
            getDataFromDb();
            prepareDataFromDb();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(CommonModuleApp.TAG_LOG_CAT, "TabsActivity onPause");
    }

    private void initializeActionBarTabs(){
        ActionBar actionBar = getActionBar();
        actionBar.removeAllTabs();

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab tab = actionBar
                .newTab()
                .setText(R.string.title_map)
                .setTabListener(new ActionBarTabListener<MapFragment>(this, "MAPA", MapFragment.class));

        actionBar.addTab(tab);

        if (isFromHistory){
            tab = actionBar
                .newTab()
                .setText(R.string.statistic)
                .setTabListener(new ActionBarTabListener<StatisticFragment>(this, "STAT", StatisticFragment.class));

            actionBar.addTab(tab);

            tab = actionBar
                    .newTab()
                    .setText(R.string.graph)
                    .setTabListener(new ActionBarTabListener<GraphFragment>(this, "GRAPH", GraphFragment.class));

            actionBar.addTab(tab);
        }
    }

    public void getDataFromDb() {
        db.open();
        dataList = db.getDataForGraph(String.valueOf(getIdRoad()));
        db.close();
    }

    protected void prepareDataFromDb() {
        // speed
        for (Double aDouble : dataList.get(DbAdapter.KEY_L_SPEED)) {
            if (this.maxSpeed < aDouble) this.maxSpeed = aDouble;
            this.speedListKm.add(aDouble*3.6);
        }

        // distance
        List<Double> distanceTemp = dataList.get(DbAdapter.KEY_L_DISTANCE);
        for (int i = 0; i < distanceTemp.size(); i++) {
            int j = i;
            double tempDistance = 0;

            for (int k = 0; k <= j; k++) {
                tempDistance += distanceTemp.get(k);
            }
            tempDistance = tempDistance / 1000;

            tempDistance = setPrecision(tempDistance, 2);

            this.distanceListSumForGraphKm.add(tempDistance);
        }

        // altitude
        List<Double> altitudeTemp = dataList.get(DbAdapter.KEY_L_ALTITUDE);
        for (Double aDouble : altitudeTemp) {
            this.altitudeListPrecision.add(setPrecision(aDouble, 2));
        }
    }

    public double setPrecision(double value, int precision) {
        BigDecimal bd = new BigDecimal(value);
        bd = bd.setScale(precision, BigDecimal.ROUND_UP);
        value = bd.doubleValue();
        return value;
    }

    public Marker getLastMarker() {
        return lastMarker;
    }

    public void setLastMarker(Marker lastMarker) {
        this.lastMarker = lastMarker;
    }

    public long getIdRoad() {
        return idRoad;
    }

    public double getFirstLatitude() {
        return firstLatitude;
    }

    public double getFirstLongitude() {
        return firstLongitude;
    }

    public boolean isFromHistory() {
        return isFromHistory;
    }

    public void setFromHistory(boolean isFromHistory) {
        this.isFromHistory = isFromHistory;
    }

    public int getMapMode() {
        return mapMode;
    }

    public void setMapMode(int mapMode) {
        this.mapMode = mapMode;
    }

    public String getRedirectType() {
        return redirectType;
    }

    public void setRedirectType(String redirectType) {
        this.redirectType = redirectType;
    }

    public Marker getFirstMarker() {
        return firstMarker;
    }

    public void setFirstMarker(Marker firstMarker) {
        this.firstMarker = firstMarker;
    }

    public List<Double> getSpeedListKm() {
        return speedListKm;
    }

    public void setSpeedListKm(List<Double> speedListKm) {
        this.speedListKm = speedListKm;
    }

    public List<Double> getDistanceListSumForGraphKm() {
        return distanceListSumForGraphKm;
    }

    public void setDistanceListSumForGraphKm(List<Double> distanceListSumForGraphKm) {
        this.distanceListSumForGraphKm = distanceListSumForGraphKm;
    }

    public List<Double> getAltitudeListPrecision() {
        return altitudeListPrecision;
    }

    public void setAltitudeListPrecision(List<Double> altitudeListPrecision) {
        this.altitudeListPrecision = altitudeListPrecision;
    }

    public double getMaxSpeed() {
        return maxSpeed;
    }

    public void setMaxSpeed(double maxSpeed) {
        this.maxSpeed = maxSpeed;
    }

    public void setIdRoad(long idRoad) {
        this.idRoad = idRoad;
    }

    public DbAdapter getDb() {
        return db;
    }

    public void setDb(DbAdapter db) {
        this.db = db;
    }

    public double getFirstAltitude() {
        return firstAltitude;
    }

    public void setFirstAltitude(double firstAltitude) {
        this.firstAltitude = firstAltitude;
    }
}
